#include "util.hpp"
#include "level.hpp"
#include "message.hpp"
#include "format.hpp"
#include "sink.hpp"
#include <unistd.h>
enum class TimeGap{
    GAP_SECOND,
    GAP_MIUTE,
    GAP_HOUR,
    GAP_DAY,
};
 class RollByTimeSink :public bitlog::LogSink{
        public:
        //传入文件路径，并且打开文件
        RollByTimeSink(const std::string& basename,TimeGap gap_type)
        :_basename(basename){
            switch(gap_type){
                case TimeGap::GAP_SECOND:_gap_size = 1;break;
                case TimeGap::GAP_MIUTE:_gap_size = 60;break;
                case TimeGap::GAP_HOUR:_gap_size = 3600;break;
                case TimeGap::GAP_DAY:_gap_size = 3600*24;break;
            }
            _cur_gap = _gap_size == 1 ? bitlog::util::Date::now() : bitlog::util::Date::now() % _gap_size;
            std::string filename = createNewFile();
            //创建日志文件所在的目录
            bitlog::util::File::createDirectory(bitlog::util::File::path(filename));
            //创建并打开日志文件
            _ofs.open(filename,std::ios::binary|std::ios::app);
            assert(_ofs.is_open());//判断文件是否打开
        }
        //写入前判断文件大小，超过了最大大小就要切换文件
            void log(const char* data,size_t len)override{
            time_t cur =bitlog::util::Date::now();
                if((cur%_gap_size)!=_cur_gap){
                    _ofs.close();
                    std::string filename = createNewFile();
                    _ofs.open(filename,std::ios::binary|std::ios::app);
                    assert(_ofs.is_open());
                }
                _ofs.write(data,len);
                assert(_ofs.good());
                
            }
        private:
            std::string createNewFile(){
                //获取系统时间，以时间来构造文件扩展名
                time_t t = bitlog::util::Date::now();
                struct tm lt;
                localtime_r(&t,&lt);//将时间戳转换为有年月日的结构
                std::stringstream filename;
                filename << _basename;
                filename << lt.tm_year+1900;
                filename << lt.tm_mon+1;
                filename << lt.tm_mday;
                filename << lt.tm_hour;
                filename << lt.tm_min;
                filename << lt.tm_sec;
                filename << ".log";
                return filename.str();
            }//进行大小判断，超过指定大小就要切换新文件
        private:
        std::string _basename;
        std::ofstream _ofs;
        size_t _cur_gap;//当前是第几个时间段
        size_t _gap_size;//时间段的大小
    };
int main(){
    bitlog::Logger::ptr()
    // bitlog::LogMsg msg(bitlog::LogLevel::value::INFO,53,"main.c","root","格式化功能测试...");
    // bitlog::Formatter fmt;
    // std::string str = fmt.format(msg);
    // bitlog::LogSink::ptr stdout_lsp = bitlog::SinkFactory::create<bitlog::StdoutSink>();
    // bitlog::LogSink::ptr file_lsp = bitlog::SinkFactory::create<bitlog::FileSink>("./logfile/test.log");
    // bitlog::LogSink::ptr roll_lsp = bitlog::SinkFactory::create<bitlog::RollBySizeSink>("./logfile/roll-",1024*1024);
    // stdout_lsp->log(str.c_str(),str.size());
    // bitlog::LogSink::ptr time_lsp = bitlog::SinkFactory::create<RollByTimeSink>("./logfile/roll-",TimeGap::GAP_SECOND);
    // // while(bitlog::util::Date::now());
    // time_t old = bitlog::util::Date::now();
    // while(bitlog::util::Date::now() < old + 5){
    //     time_lsp->log(str.c_str(),str.size());
    //     usleep(1000);
    // }
    // file_lsp->log(str.c_str(),str.size());
    // size_t cursize = 0;
    // size_t count = 0;
    // while(cursize<1024*1024*10){
    //     std::string tmp = str+std::to_string(count++);
    //     cursize+=tmp.size();
    //     roll_lsp->log(tmp.c_str(),tmp.size());
    // }
    // std::cout<<bitlog::LogLevel::toString(bitlog::LogLevel::value::DEBUG)<<std::endl;
    // std::cout<<bitlog::LogLevel::toString(bitlog::LogLevel::value::ERROR)<<std::endl;
    // std::cout<<bitlog::LogLevel::toString(bitlog::LogLevel::value::FATAL)<<std::endl;
    // std::cout<<bitlog::LogLevel::toString(bitlog::LogLevel::value::INFO)<<std::endl;
    // std::cout<<bitlog::LogLevel::toString(bitlog::LogLevel::value::OFF)<<std::endl;
    // std::cout<<bitlog::LogLevel::toString(bitlog::LogLevel::value::UNKNOW)<<std::endl;
    // std::cout<<bitlog::LogLevel::toString(bitlog::LogLevel::value::WARN)<<std::endl;
// std::cout<<bitlog::util::Date::now()<<std::endl;
// std::string pathname = "./abc/bcd/a.txt";
// bitlog::util::File::createDirectory(bitlog::util::File::path(pathname));
return 0;
}